﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;

namespace EPE.DALFactory
{
    public sealed class DataAccessFactory
    {
        private static readonly string AssemblyPath = ConfigurationManager.AppSettings["DAL"];

        #region CreateObject

        //不使用缓存
        private static object CreateObjectNoCache(string AssemblyPath, string classNamespace)
        {
            try
            {
                object objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);
                return objType;
            }
            catch//(System.Exception ex)
            {
                //string str=ex.Message;// 记录错误日志
                return null;
            }

        }
        //使用缓存
        private static object CreateObject(string AssemblyPath, string classNamespace)
        {
            object objType = DataCache.GetCache(classNamespace);
            if (objType == null)
            {
                try
                {
                    objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);
                    DataCache.SetCache(classNamespace, objType);// 写入缓存
                }
                catch//(System.Exception ex)
                {
                    //string str=ex.Message;// 记录错误日志
                }
            }
            return objType;
        }
        #endregion

        public static EPE.IDAL.IAuthentication CreateAuthentication()
        {

            string ClassNamespace = AssemblyPath + ".AuthenticationImpl";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (EPE.IDAL.IAuthentication)objType;
        }

        public static EPE.IDAL.IUserMgr CreateUserMgr()
        {

            string ClassNamespace = AssemblyPath + ".UserMgrImpl";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (EPE.IDAL.IUserMgr)objType;
        }

        public static EPE.IDAL.IShopsProject CreateShopsProject()
        {

            string ClassNamespace = AssemblyPath + ".ShopsProjectImpl";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (EPE.IDAL.IShopsProject)objType;
        }

        public static EPE.IDAL.IShopsBuild CreateShopsBuild()
        {

            string ClassNamespace = AssemblyPath + ".ShopsBuildImpl";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (EPE.IDAL.IShopsBuild)objType;
        }

        public static EPE.IDAL.IShops CreateShops()
        {

            string ClassNamespace = AssemblyPath + ".ShopsImpl";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (EPE.IDAL.IShops)objType;
        }


        public static EPE.IDAL.IShopsPricePara CreateShopsPricePara()
        {

            string ClassNamespace = AssemblyPath + ".ShopsPriceParaImpl";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (EPE.IDAL.IShopsPricePara)objType;
        }

    }
}
